package com.zhj.leetcode;

/**
 * 力扣11
 * 盛水最多容器
 * @author zhj
 * @create 2024-02-18-16:12
 */
public class Test11 {

    public static void main(String[] args) {
        int[] height = new int[]{1,8,6,2,5,4,8,3,7};
        System.out.println(maxArea2(height));
    }

    public static int maxArea1(int[] height) {
        int r = 0;
        for (int i = 0; i < height.length - 1; i++) {
            for (int j = i + 1; j < height.length; j++) {
                int x = j - i;
                int y = height[i];
                y = Math.min(y, height[j]);
                r = Math.max(r, x * y);
            }
        }
        return r;
    }

    public static int maxArea2(int[] height) {
        int res = 0; int l = 0; int r = height.length - 1;
        while (l < r) {
            res = Math.max(res, (r - l) * Math.min(height[l], height[r]));
            if(height[l] <= height[r]) {
                l++;
            } else {
                r--;
            }
        }
        return res;
    }

}
